package com.javasm.sys.controller;

import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.dict.service.DicDataService;
import com.javasm.sys.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("users")
public class UserController extends BaseController {

    private static Logger l = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private DicDataService dicDataService;

    @Autowired
    private PosService posService;

    @PostMapping
    public AxiosResult addUser(@RequestBody SysUser user) {
        Integer insert = userService.addUser(user);
        return isok(insert == 1);
    }

    @GetMapping("resetPwd/{userId}")
    public AxiosResult resetPwd(@PathVariable String userId) {
        Integer update = userService.resetPwd(userId);
        return isok(update == 1);
    }

    //    resetPwd 重置密码
    @GetMapping("AllDept")
    public AxiosResult getAllDept() {
        List<SysDepart> sysDeparts = userService.buildDeptTree();
        return AxiosResult.suc(sysDeparts);
    }

    @GetMapping("AllPos")
    public AxiosResult getAllPos() {
        List<SysPos> sysPoses = posService.getList();
        return AxiosResult.suc(sysPoses);
    }

    @GetMapping("getMenus/{userId}")
    public AxiosResult getMenus(@PathVariable String userId) {
        List<SysMenu> menus = userService.getMenus(userId);
        return AxiosResult.suc(menus);
    }

    @GetMapping
    public TableDatas getUsers(SysUser user) {
        startPage();
        List<SysUser> list = userService.getUsers(user);
        return new TableDatas(list);
    }

    @GetMapping("{userId}")
    public AxiosResult getParms(@PathVariable String userId) {
        SysUser parms = userService.getParms(userId);
        return AxiosResult.suc(parms);
    }

    @PutMapping
    public AxiosResult updateUsers(@RequestBody SysUser user) {
        l.info(user.toString());
        Integer flag = userService.updateUserById(user);
        return isok(flag == 1);
    }

    @DeleteMapping("{userId}")
    public AxiosResult delUserById(@PathVariable String userId) {
        Integer flag = userService.delUserById(userId);
        return isok(flag == 1);
    }

    @DeleteMapping("delUsers/{ids}")
    public AxiosResult delUserById(@PathVariable List<String> ids) {
        Integer flag = userService.delUserByIds(ids);
        return isok(flag == ids.size());
    }

}
